15. Exercise: Java Object Serialization
Exercise: Java Object Serialization
Remember the UdacisearchClient
"bean" class you used in a previous exercise?
Suppose you need to save a client's information to a file so that it can stick around on a hard drive until the heat death of the universe. How would you do this with Java Serialization?
You are going to write a program to try it out!
- Make the
UdacisearchClient
class extend thejava.io.Serializable
interface. - Next, in
Main.java
create anObjectOutputStream
and callObjectOutputStream#writeObject(client)
to serialize theclient
UdacisearchClient
to theoutputPath
. - Finally, read in the file you just created using
ObjectInputStream#readObject()
(you'll have to case the result to aUdacisearchClient
). If you want, you can print the deserialized object to standard output to make sure it worked. - Now, run the program:
javac Main.java
java Main client.bin
Note: The ".bin" suffix in "client.bin" doesn't actually do anything special; in this example it just makes it so that people looking at the file name will be able to guess that the final contains binary data.
TODO List
Task Feedback:
Nice work!
Code
If you need a code on the https://github.com/udacity.
export PATH=/data/jdk-15.0.1/bin:$PATH
export JAVA_HOME=/data/jdk-15.0.1/bin